package com.aaa.vegetablemarketexpress.service.impl;

import com.aaa.vegetablemarketexpress.dao.GradeDao;
import com.aaa.vegetablemarketexpress.entity.Grade;
import com.aaa.vegetablemarketexpress.entity.User;
import com.aaa.vegetablemarketexpress.service.GradeService;
import com.aaa.vegetablemarketexpress.util.CustomException;
import com.aaa.vegetablemarketexpress.util.ResultStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @FileName: GradeServiceImpl
 * @Description: 商品等级服务实现类
 * @Author:
 * @CreateTime: 2025/9/9
 * @Version: 1.0.0
 */
@Service
public class GradeServiceImpl implements GradeService {

    @Resource
    private HttpSession session;
    @Resource
    private GradeDao gradeDao;

    /**
     * 根据ID查询等级
     * @param gradeId
     * @return
     */
    @Override
    public Grade selectGradeById(Long gradeId) {
        return gradeDao.selectGradeById(gradeId);
    }

    /**
     * 查询商品等级列表
     * @param grade
     * @return
     */
    @Override
    public List<Grade> selectGradeList(Grade grade) {
        return gradeDao.selectGradeList(grade);
    }

    /**
     * 插入商品等级
     * @param grade
     * @return
     */
    @Override
    public int insertGrade(Grade grade) {
        grade.setCreateTime(new Date());
        grade.setDelFlag("0");

        // 检查等级名称是否唯一
        if (!"0".equals(checkGradeNameUnique(grade))) {
            throw new CustomException(ResultStatus.FAILED.getStatusCode(), "等级名称已存在");
        }
        grade.setCreateBy(((User)(session.getAttribute("userInfo"))).getUserName() );
        grade.setCreateTime(new Date());
        return gradeDao.insertGrade(grade);
    }

    /**
     * 更新商品等级
     * @param grade
     * @return
     */
    @Override
    public int updateGrade(Grade grade) {
        grade.setUpdateTime(new Date());
        // 检查等级名称是否唯一
        if (!"0".equals(checkGradeNameUnique(grade))) {
            throw new CustomException(ResultStatus.FAILED.getStatusCode(), "等级名称已存在");
        }
        grade.setUpdateBy(((User)(session.getAttribute("userInfo"))).getUserName() );
        grade.setUpdateTime(new Date());
        return gradeDao.updateGrade(grade);
    }

    /**
     * 删除商品等级
     * @param gradeId
     * @return
     */
    @Override
    public int deleteGradeById(Long gradeId) {
        // 检查等级是否被使用
        if (gradeDao.checkGradeUse(gradeId) > 0) {
            throw new CustomException(ResultStatus.FAILED.getStatusCode(), "等级已被使用，不能删除");
        }
        return gradeDao.deleteGradeById(gradeId);
    }

    /**
     * 批量删除商品等级
     * @param gradeIds
     * @return
     */
    @Override
    public int deleteGradeByIds(Long[] gradeIds) {
        for (Long gradeId : gradeIds) {
            // 检查等级是否被使用
            if (gradeDao.checkGradeUse(gradeId) > 0) {
                throw new CustomException(ResultStatus.FAILED.getStatusCode(),
                        "等级ID " + gradeId + " 已被使用，不能删除");
            }
        }
        return gradeDao.deleteGradeByIds(gradeIds);
    }

    /**
     * 检查等级名称是否唯一
     * @param grade
     * @return
     */
    @Override
    public String checkGradeNameUnique(Grade grade) {
        Grade info = gradeDao.selectGradeByName(grade.getGradeName());
        if (info != null && !info.getGradeId().equals(grade.getGradeId())) {
            return "1"; // 已存在
        }
        return "0"; // 不存在
    }

    /**
     * 更新商品等级状态
     * @param gradeId
     * @param status
     * @return
     */
    @Override
    @Transactional
    public int updateGradeStatus(Long gradeId, String status) {
        return gradeDao.updateGradeStatus(gradeId, status);
    }

    /**
     * 查询所有正常商品等级
     * @return
     */
    @Override
    public List<Grade> selectAllNormalGrade() {
        return gradeDao.selectAllNormalGrade();
    }

    /**
     * 查询商品等级总数
     * @param grade
     * @return
     */
    @Override
    public int countGrade(Grade grade) {
        return gradeDao.countGrade(grade);
    }
}

